package com.cidp.system.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.TopRole;
import com.cidp.system.model.ToperatorDetail;
import com.cidp.system.model.Trole;
import com.cidp.system.model.TrolePermission;
import com.cidp.system.pojo.OperatorDetailPo;
import com.cidp.system.pojo.PermissionPo;
import com.cidp.system.pojo.RolePo;
import com.cidp.system.service.IOpRoleService;
import com.cidp.system.service.IPermissionService;
import com.cidp.system.service.IRolePermissionService;
import com.cidp.system.service.IRoleService;
import com.litt.core.common.Constants;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;
/**
 * <b>标题：</b>角色管理action类.
 * <pre><b>描述</b>
 *    操作员管理
 * </pre>
 * 
 * @author zhanglei 
 * @since 2008-6-6
 * @version 1.0
 */
public class RoleAction extends DispatchAction{
	private IRoleService roleService;
	private IOpRoleService opRoleService;
	private IPermissionService permissionService;
	private IRolePermissionService rolePermissionService;

	/**
	 * 查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		RolePo rolePo=(RolePo)dynaActionForm.get("rolePo");
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = roleService.listByPage(new PagePoParam(rolePo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("roleList");
	}
	/**
	 * 查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward searchroleper(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		RolePo rolePo=(RolePo)dynaActionForm.get("rolePo");
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = roleService.listByPage(new PagePoParam(rolePo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("roleperList");
	}
	/**
	 * 增加
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("roleAdd");
	}
	/**
	 * 保存
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		RolePo rolePo=(RolePo)dynaActionForm.get("rolePo");
		Trole role=new Trole();
		role.setRoleName(rolePo.getRoleName());
		role.setStatus(rolePo.getStatus());
		role.setRemark(rolePo.getRemark());
		roleService.save(role);
		return mapping.findForward("success");
	}
	/**
	 * 編輯
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		RolePo rolePo=(RolePo)dynaActionForm.get("rolePo");
		String sId=request.getParameter("id");
		Long id=new Long(sId);
		Trole role=roleService.load(id);
		BeanUtils.copyProperties(rolePo,role);
		dynaActionForm.set("rolePo", rolePo);
		return mapping.findForward("roleEdit");
	}
	/**
	 * 修改
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		RolePo rolePo=(RolePo)dynaActionForm.get("rolePo");
		Trole role=roleService.load(rolePo.getRoleId());
		role.setRoleName(rolePo.getRoleName());
		role.setStatus(rolePo.getStatus());
		role.setRemark(rolePo.getRemark());
		roleService.update(role);
		return mapping.findForward("success");
	}
	public ActionForward searchrole(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		RolePo rolePo=(RolePo)dynaActionForm.get("rolePo");
		String opId=request.getParameter("id");
		List opRoleList=opRoleService.loadRoleById(new Long(opId));
		Limit limit=ExtremeTableUtil.getLimit(request,9999);
		IPageList page = roleService.listByPage(new PagePoParam(rolePo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		request.setAttribute("opRoleList",opRoleList);
		return mapping.findForward("oproleList");
	}
	public ActionForward updaterole(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		String opId=(String)dynaActionForm.get("opId");
		String[] ids=(String[])dynaActionForm.get("ids");
		opRoleService.deleteByIds(new Long(opId));
		for(int i=0;i<ids.length;i++){
			TopRole opRole=new TopRole();
			opRole.setOpId(new Long(opId));
			opRole.setRoleId(new Long(ids[i]));
			opRoleService.save(opRole);
		}
		return mapping.findForward("success");
	}
	public ActionForward searchpermission(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		PermissionPo permissionPo=(PermissionPo)dynaActionForm.get("permissionPo");
		String roleId=request.getParameter("id");
		List rolePermissionList=rolePermissionService.loadPermissionById(new Long(roleId));
		Limit limit=ExtremeTableUtil.getLimit(request,9999);
		IPageList page = permissionService.listByPage(new PagePoParam(permissionPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		request.setAttribute("rolePermissionList",rolePermissionList);
		return mapping.findForward("rolepermissionList");
	}
	public ActionForward updateperssion(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		String roleId=(String)dynaActionForm.get("roleId");
		String[] ids=(String[])dynaActionForm.get("ids");
		rolePermissionService.deleteByIds(new Long(roleId));
		for(int i=0;i<ids.length;i++){
			TrolePermission rolePermission=new TrolePermission();
			rolePermission.setRoleId(new Long(roleId));
			rolePermission.setPermissionCode(ids[i]);
			rolePermissionService.save(rolePermission);
		}
		return mapping.findForward("success");
	}
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public void setOpRoleService(IOpRoleService opRoleService) {
		this.opRoleService = opRoleService;
	}
	public void setRolePermissionService(IRolePermissionService rolePermissionService) {
		this.rolePermissionService = rolePermissionService;
	}
	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}
}
